/*
#	Fastfoo - CMS developer under CakePHP / http://fast-foo.com
#	Copyright (C) 2010  Antonio Irlandés García

#	This program is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.

#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.

#	You should have received a copy of the GNU General Public License
#	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


//Selecciona/Deselecciona todos los check
//de los listados del panel de administración
function checkAll(idForm,bool){
	for (var i=0;i<$(idForm).elements.length;i++){
		var element = $(idForm).elements[i];
 			if (element.type == "checkbox" && element.name == "list_reg[]"){
				if(bool && element.checked == false){
					element.checked = true;
					//Cambio los estilos de la fila
					$('reg_' + element.id).toggleClassName('checked');
				}else if(!bool && element.checked == true){
					element.checked = false;
					//Cambio los estilos de la fila
					$('reg_' + element.id).toggleClassName('checked');
				}

			}
	}
	
}


//Selecciona/Deselecciona todos los check de los permisos
function checkAllPermissions(id){
	//array de todos los elements que cumplan esa instruccion
	var list_chk = $$('#registers_'+ id +' li input');

	//vacio a todos de su la clase indicada	
	for(var i=0; i<list_chk.length; i++){
		var element = list_chk[i];
		if (element.type == "checkbox"){
			if(element.checked == false){
				element.checked = true;
				element.disabled = true;
			}else if(element.checked == true){
				element.checked = false;
				element.disabled = false;
			}
		}		
	}
	
}

//Pide una confirmacion a la hora de eliminar
function alertDel(idForm, msg, msg_error){
	if(verCheck(idForm)){
		if (confirm(msg))//Si pulsamos sobre aceptar
			$(idForm).submit();
	}else{
		alert(msg_error);
	}
}

//Pide una confirmacion a la hora de vaciar los logs
function alertEmpty(idForm, msg){
	if (confirm(msg))//Si pulsamos sobre aceptar
			$(idForm).submit();
}

//Verifica si ha sido chekeado algun checkbox de la lista
//de registros del panel de administración
function verCheck(idForm){
	for(i=0; i<$(idForm).elements.length; i++){
		if($(idForm).elements[i].type == "checkbox" && $(idForm).elements[i].name == "list_reg[]" && $(idForm).elements[i].checked == true)
			return true;
	}
}

//Detecta la pulsación del enter y redirige  a la accion indicada
//Usada en el buscador
function checkEnter(e,url_action){ //e is event object passed from function invocation
	var characterCode //literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
	  e = e
	  characterCode = e.which //character code is contained in NN4's which property
	}else{
	  e = event
	  characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
	  eval("document.location='" + url_action + "'");
	  return false
	}else{
	  return true
	}
}

//Menu de salto empleado para las listas en el panel de administracion (elegir el numero de registros por página)
//Empleo la URL "bonita" a la hora de redireccionar
function JumpMenu(url,selObj,restore){
	eval("document.location='" + url + "/" + selObj.name + ":" + selObj.options[selObj.selectedIndex].value + "'");
	if (restore) selObj.selectedIndex=0;
}



